package databasetest;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	Connection conn;
	Select s;
	
	Delete d;
	private JLabel label;
	private JTextField code;
	private JLabel label_1;
	private JTextField ea;
	private JButton modify;
	private JLabel label_2;
	private JTextField serial;
	private JButton search;
	private JLabel result;
	
	private JButton button;

	public Update(Connection c) {
		super("Update",true,true,true,false);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 272, 176);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getCode());
		getContentPane().add(getLabel_1());
		getContentPane().add(getEa());
		getContentPane().add(getModify());
		getContentPane().add(getLabel_2());
		getContentPane().add(getSerial());
		getContentPane().add(getSearch());
		getContentPane().add(getResult());
		getContentPane().add(getButton());
		this.conn=c;

	}
	public void setU(Select u){
		this.s=u;
	}
	

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드");
			label.setBounds(12, 53, 41, 23);
		}
		return label;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(60, 53, 102, 23);
		}
		return code;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량");
			label_1.setBounds(12, 82, 41, 23);
		}
		return label_1;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(60, 82, 102, 23);
		}
		return ea;
	}
	public void Modify(){
		int s= Integer.parseInt(serial.getText());
		String c=code.getText();
		int e=Integer.parseInt(ea.getText());
		
		String sql= "update ptbl set code=?, ea=?  where serial=?";
		try{
			PreparedStatement pstmt= conn.prepareStatement(sql);
			pstmt.setString(1, c);
			pstmt.setInt(2, e);
			pstmt.setInt(3, s);
			
			pstmt.executeUpdate();
			pstmt.close();
		}catch(Exception ex){
			
		}
	}
	private JButton getModify() {
		if (modify == null) {
			modify = new JButton("수정");
			modify.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				Modify();
				s.columnadd();
					
				}
			});
			modify.setBounds(174, 67, 69, 33);
		}
		return modify;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("시리얼");
			label_2.setBounds(12, 24, 41, 23);
		}
		return label_2;
	}
	public JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(60, 24, 102, 23);
		}
		return serial;
	}
	private JButton getSearch() {
		if (search == null) {
			search = new JButton("검색");
			search.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					search1();
				}
			});
			search.setBounds(174, 24, 69, 33);
		}
		return search;
	}
	public void search1(){
		try{
			int s=Integer.parseInt(serial.getText());
			
			String sql="Select * from ptbl where serial=?";
			PreparedStatement pstmt= conn.prepareStatement(sql);
			pstmt.setInt(1,s);
			ResultSet rs = pstmt.executeQuery();
			
			if(rs.next()){
				code.setText(rs.getString("code"));
				ea.setText(""+rs.getInt("ea"));
				result.setText("데이터가 입력되었습니다.");
			}else{
				result.setText("데이터가 없습니다.");
			}
			rs.close();
			pstmt.close();
			
		}catch(Exception e){
			e.getMessage();
		}
	}
	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("검색할 코드를 입력하세요...");
			result.setBounds(12, 115, 162, 22);
		}
		return result;
	}

		public void dele(){
			int s= Integer.parseInt(serial.getText());

			
			String sql= "delete from ptbl where serial=?";
			try{
				PreparedStatement pstmt= conn.prepareStatement(sql);
				pstmt.setInt(1, s);
				
				pstmt.executeUpdate();
				pstmt.close();
				serial.setText("");
				code.setText("");
				ea.setText("");
				
			}catch(Exception ex){
				ex.getMessage();
				
			}
		}
	
	private JButton getButton() {
		if (button == null) {
			button = new JButton("삭제");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dele();
					s.columnadd();
					
				}
			});
			button.setBounds(174, 104, 69, 33);
		}
		return button;
	}
}
